package com.hns.can.master.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseLineInfoEntity;
import com.hns.can.master.service.LineInfoService;
import com.hns.can.master.vo.LinePostDataVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;

/**
 * 线路信息维护控制类
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/lineInfo")
public class LineInfoController extends BaseController {
    
    @Autowired
    private LineInfoService lineInfoService; // 注入lineInfoService
    
    /**
	 * 导出文件表头
	 */
    private final String[] tableHeaderNames={"编码","线路名称","所属机构","上行里程","下行里程","上行站点数","下行站点数","线路类型","无人售票","起步价"};
    /**
	 * 导出文件字段名
	 */
    private final String[] tableHeaderFields = {"lineCode","lineName","organName","lineUpstreMile","lineDostreMile","upstreStnNum","dostreStnNum","lineTypeId","wthrUnmannedTicket","startPrice"};
    
    /**
     * 添加一个线路
     * @param entity 线路信息
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/addLineInfo")
    @ResponseBody
    public Object addLineInfo(BaseLineInfoEntity entity) throws Exception {
        int countCode= lineInfoService.getlineInfoByLineCode(entity);
        if(countCode>0){
            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的线路编码已存在");
        }
        int count= lineInfoService.getlineInfoByLineName(entity);
        if(count > 0){
            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的线路名称已存在");
        }else{
            lineInfoService.addLineInfo(entity, this.getCurrentUser().getUser());
            return this.getReturnMsg("您成功添加一条线路");
        }
    }
    
    /**
     * 得到线路的page对象
     * @param dataVo 查询条件
     * @return  操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/searchLineInfoPage")
    @ResponseBody
    public Object searchLineInfoPage(LinePostDataVo dataVo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        dataVo.setUserId(user.getId());
        Pagination page = lineInfoService.searchLineInfoPage(dataVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 导出线路信息excel
     * @param dataVo 查询条件
     * @param response
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/exportLineInfoExcel")
    @ResponseBody
    public void exportLineInfoExcel(LinePostDataVo dataVo, HttpServletResponse response) throws Exception {
        dataVo.setUserId(this.getCurrentUser().getUser().getId());
        List<BaseEntity> list = lineInfoService.getLineInfoList(dataVo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "LineInfo", "线路基础信息");
    }
    
    /**
     * 导出线路信息pdf
     * @param dataVo 查询条件
     * @param response
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/exportLineInfoPdf")
    @ResponseBody
    public void exportLineInfoPdf(LinePostDataVo dataVo, HttpServletResponse response) throws Exception {
        dataVo.setUserId(this.getCurrentUser().getUser().getId());
        List<BaseEntity> list = lineInfoService.getLineInfoList(dataVo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "LineInfo", "线路基础信息");
    }
    /**
     * 根据线路id求线路信息
     * @param lineId 线路id
     * @param response 
     * @return 线路信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getLineInfoByLineId")
    @ResponseBody
    public Object getLineInfoByLineId(@RequestParam(value="lineId") String lineId) throws Exception {
        BaseLineInfoEntity entity = lineInfoService.getLineInfoByLineId(lineId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 更新线路的信息
     * @param entity 线路信息
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/uptLineInfo")
    @ResponseBody
    public Object uptLineInfo(BaseLineInfoEntity entity) throws Exception {
        lineInfoService.uptLineInfo(this.getCurrentUser().getUser(), entity);
        return this.getReturnMsg("保存成功");
    }
    
    /**
     * 删除一个线路
     * @param lineId 线路id
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/delLineInfo")
    @ResponseBody
    public Object delLineInfo(@RequestParam(value="lineIds") String[] lineIds) throws Exception {
        lineInfoService.delLineInfo(this.getCurrentUser().getUser(), lineIds);
        return this.getReturnMsg("线路删除成功");
    }
    
}
